package com.azt.provider.impl;

import com.azt.api.pojo.Category;
import com.azt.api.pojo.Pcate;
import com.azt.api.pojo.WordSql;
import com.azt.api.service.ProCategoryService;
import com.azt.api.service.UseForDevelopService;
import com.azt.model.page.Pagination;
import com.azt.provider.mapper.UseForDevelopMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.*;
import org.jeecgframework.poi.util.PoiExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@Transactional
public class UseForDevelopServiceImpl implements UseForDevelopService {

	@Autowired
	private ProCategoryService procategoryservice;
	@Autowired
	private UseForDevelopMapper useForDevelopMapper;
	
	@Override
	public void analyProductType(List<Map<String, Integer>> fcatelist, List<Map<String, Integer>> scatelist,
			String uploadFile) throws Exception {
		String[] tcate;
		StringBuffer sb=new StringBuffer();
		sb.append(uploadFile.substring(0,uploadFile.indexOf("admin")+6)).append(URLEncoder.encode(uploadFile.substring(uploadFile.indexOf("admin")+6,uploadFile.lastIndexOf("xlsx")),"utf-8")).append("xlsx");
		Workbook workbook = PoiExcelUtil.toWorkbook(sb.toString());
		Sheet sheet = workbook.getSheetAt(2);
		//int sheetMergeCount = sheet.getNumMergedRegions();// 合并单元格的数量
		int rowCount = sheet.getPhysicalNumberOfRows();
		//按行读写
		List<Category> category1=new ArrayList();
		List<Category> category2=new ArrayList();
		List<Category> category3=new ArrayList();
		Map<String,Object> fmap=new HashMap();
		Map<String,Object> smap=new HashMap();
		Map<String,Object> tmap=new HashMap();
		String fcode=null;
		String scode=null;
		String tcode=null;
		
		for(int r = 1; r < rowCount; r++){
			    Row row = sheet.getRow(r);
			    //第一级数据
				String name1 = row.getCell(0).getRichStringCellValue()!=null?row.getCell(0).getRichStringCellValue().toString():"";
				String code1 = row.getCell(1).getRichStringCellValue()!=null?row.getCell(1).getRichStringCellValue().toString():"";
				//第二级数据
				String name2 = row.getCell(2).getRichStringCellValue()!=null?row.getCell(2).getRichStringCellValue().toString():"";
				String code2 = row.getCell(3).getRichStringCellValue()!=null?row.getCell(3).getRichStringCellValue().toString():"";
				//第三级数据
				String name3 = row.getCell(4).getRichStringCellValue()!=null?row.getCell(4).getRichStringCellValue().toString():"";
				String code3 = row.getCell(5).getRichStringCellValue()!=null?row.getCell(5).getRichStringCellValue().toString():"";
		
				fcode= fmap.get("fcode")!=null?fmap.get("fcode").toString():null;
				scode= smap.get("scode")!=null?smap.get("scode").toString():null;
				tcode= tmap.get("tcode")!=null?tmap.get("tcode").toString():null;;
			
				
				if(r==1 || fcode!=null && !fcode.equals(code1)){
					category2=new ArrayList();
					Category pro = new Category();
					pro.setName(name1.trim());
					pro.setCode(code1.trim());
					pro.setCateType(1);
					pro.setPid(0);
					category1.add(pro);
					fmap.put("fcode", code1);
				}
				
				if(r==1 || scode!=null && !scode.equals(code2)){
					category3=new ArrayList();
					Category pro = new Category();
					pro.setName(name2.trim());
					pro.setCode(code2.trim());
					pro.setCateType(2);
					category2.add(pro);
					smap.put("scode", code2);
				}
				
				if(fcode!=null && fcode.equals(code1)){
					fmap.put(code1, category2);
				}
				
				
				if(r==1 || tcode!=null && !tcode.equals(code3)){
					if(StringUtils.isNotBlank(name3)){
						Category pro = new Category();
						pro.setName(name3.trim());
						pro.setCode(code3.trim());
						pro.setCateType(3);
						category3.add(pro);
						tmap.put("tcode", code3);
					}
				}
				
				if(scode!=null && scode.equals(code2)){
					smap.put(code2, category3);
				}
				
		}
		
		//依次保存数据
		
		for(Category c1:category1){
			procategoryservice.save(c1);
		}
		
		for(Category c1:category1){
			List<Category> category21 = (List<Category>) fmap.get(c1.getCode());
			if(category21!=null && category21.size()>0){
				for(Category c21:category21){
					c21.setPid(c1.getId());
					procategoryservice.save(c21);
				}
			}
			
		}
		
		for(Category c1:category1){
			List<Category> category21 = (List<Category>) fmap.get(c1.getCode());
			if(category21!=null && category21.size()>0){
			for(Category c21:category21){
				List<Category> category32 =(List<Category>) smap.get(c21.getCode());
				if(category32!=null && category32.size()>0){
				for(Category c32:category32){
					c32.setPid(c21.getId());
					procategoryservice.save(c32);
				 }
				}
			  }
		   }
		}
		
		//第一张表读取完后 更新老表的id
		
		/**开始读取第二张表数据**/
		Sheet sheet2 = workbook.getSheetAt(0);
		int rowCount2 = sheet2.getPhysicalNumberOfRows();
		for(int r = 1; r < rowCount2; r++){
		    Row row = sheet2.getRow(r);
		        row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
		        row.getCell(3).setCellType(Cell.CELL_TYPE_STRING);
		        row.getCell(4).setCellType(Cell.CELL_TYPE_STRING);
		        row.getCell(7).setCellType(Cell.CELL_TYPE_STRING);
		        //第一级数据
				String id1 = row.getCell(0).getRichStringCellValue().toString();
				//第二级数据
				String id2 = row.getCell(3).getRichStringCellValue().toString();
				//第三级数据
				RichTextString id3 = row.getCell(4).getRichStringCellValue();
				RichTextString code3 = row.getCell(7).getRichStringCellValue();
				
				if(code3!=null && id3!=null){
					
				Category categorys3 = procategoryservice.getCategorysByCateCode(code3.toString());
				
				if(categorys3!=null){
					Pcate pcateById = procategoryservice.getPcateById(Integer.parseInt(id3.toString()));
					if(pcateById!=null){
						pcateById.setNewid(categorys3.getId().toString());
						procategoryservice.updatePcate(pcateById);
					}
				}
				
			}
				
		}
		
		
		
	}

	@Override
	public Pagination getSqlList(int pageNo, int pageSize) {
		Page<Object> startPage = PageHelper.startPage(pageNo, pageSize);
		List<WordSql> selectAll = useForDevelopMapper.selectAll();
		
		Pagination  p = new Pagination(startPage,selectAll);
		return p;
	}

	@Override
	public void saveOrUpdate(WordSql wordsql) {
		if(wordsql.getId()!=null){
			useForDevelopMapper.updateByPrimaryKey(wordsql);
		}else{
			useForDevelopMapper.insertSelective(wordsql);
		}
	}

	@Override
	public WordSql getWordSql(Integer id) {
		return useForDevelopMapper.selectByPrimaryKey(id);
	}

	@Override
	public void delWordSql(WordSql wordSql) {
		if(wordSql != null) {
			useForDevelopMapper.delete(wordSql);
		}
	}

}
